package com.hyg.lib_music.presenter;

import android.content.Context;

import com.hyg.lib_common.DataModel.Music.FavMusic;
import com.hyg.lib_common.DataModel.Music.response.BaseResponse;
import com.hyg.lib_common.DataModel.Music.response.FavoriteListReponse;
import com.hyg.lib_music.network.NetManager;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Single;

public class MusicFavListPresenter implements MusicFavListContract.Presenter{

    private MusicFavListContract.View mFragment;
    private CloseableRxServiceExecutor executor;

    @Inject
    public MusicFavListPresenter(Context context,
                                 CloseableRxServiceExecutor executor) {
        this.executor = executor;
    }

    @Override
    public void setContractView(MusicFavListContract.View fragment) {
        mFragment = fragment;
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDestroy() {
    }

    /**
     * 音乐列表
     *
     * @return
     */
    public Single<FavoriteListReponse> musicFavListRequest() {

        return Single.create(emitter -> emitter.onSuccess(NetManager.sharedManager().musicFavListRequest()));

    }

    /**
     * 从我喜欢删除
     *
     * @return
     */
    public Single<BaseResponse> removeFavoriteRequest(int musicId) {

        return Single.create(emitter -> emitter.onSuccess(NetManager.sharedManager().removeFavoriteRequest(musicId)));

    }

    @Override
    public void getMusicFavList(){

        executor.execute(

                this.musicFavListRequest(),
                response -> {
                    if (response.code == 200)
                    {
                        List<FavMusic> data= response.data;
                        mFragment.setMusicFavListData(data);
                    }else {
                        //失败逻辑
                    }
                },
                error -> {
//                    Logger.d(error);
                });
    }


    @Override
    public void removeFavorite(int musicId) {

        executor.execute(

                this.removeFavoriteRequest(musicId),
                response -> {
                    if (response.code == 200)
                    {
                        mFragment.removeFavorite(musicId);
                    }else {
                        //失败逻辑
                    }
                },
                error -> {
//                    Logger.d(error);
                });
    }

}